//
//  ST_MatchBeforePropsItemView.swift
//  btvideo
//
//  Created by mac on 2020/7/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyUserDefaults

class ST_MatchBeforePropsItemView: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(TIANCompatibleValue(value: 60))
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 30))
            make.top.equalToSuperview().offset(TIANCompatibleValue(value: 25))
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            
            make.left.top.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.height.equalTo(TIANCompatibleValue(value: 25))
        }
        
        self.addSubview(location_image_view)
        location_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right).offset(TIANCompatibleValue(value: 5))
            make.bottom.equalTo(titleLabel).offset(-TIANCompatibleValue(value: 2))
            make.width.height.equalTo(TIANCompatibleValue(value: 18))
        }
        
        self.addSubview(location_label)
        location_label.snp.makeConstraints { (make) in
            make.left.equalTo(location_image_view.snp.right)
            make.bottom.equalTo(titleLabel).offset(-TIANCompatibleValue(value: 1))
            make.height.equalTo(TIANCompatibleValue(value: 20))
        }
        
        
        self.addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(TIANCompatibleValue(value: 2))
            make.height.equalTo(TIANCompatibleValue(value: 17))
        }
        
        
        self.addSubview(priceView)
        priceView.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(sub_title_label.snp.bottom).offset(10/375.0*ScreenWidth)
            make.height.equalTo(ST_MatchCoinView.height)
        }
        
        
        self.addSubview(action_btn)
        action_btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 15))
            make.height.equalTo(TIANCompatibleValue(value: 36))
            make.top.equalTo(priceView.snp.bottom).offset(TIANCompatibleValue(value: 10))
        }
        
        
        self.addSubview(action_sub_label)
        action_sub_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(action_btn.snp.bottom).offset(TIANCompatibleValue(value: 5))
            make.height.equalTo(TIANCompatibleValue(value: 14))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - Bind Model
    var model:ST_MatchBeforePropsItemViewModel?{
        didSet{
            
            guard let m = model else { return  }
            
            
            titleLabel.text = m.title
            sub_title_label.text = m.subTitle
            
            if let loc = m.location,loc.count > 0{
                location_label.text = loc
                location_image_view.isHidden = false
            }else{
                location_image_view.isHidden = true
            }
            
            priceView.price = m.price
            
            action_btn.setTitleColor(m.tintColor, for: UIControl.State.normal)
            
            cover_image_view.image = m.backgroundImage
            
            icon_image_view.sd_setImage(with: m.iconUrl.url, completed: nil)
            
        }
    }
    
    
//    MARK: - Properties

    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
    }()
    
    fileprivate lazy var icon_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 18/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var location_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 14/375.0*ScreenWidth, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value: 12), weight: UIFont.Weight.regular)
        return control
    }()

    fileprivate lazy var location_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("props_location_icon"))
          return control
    }()
    
    fileprivate lazy var priceView:ST_MatchCoinView = {
        
        let control  = ST_MatchCoinView()
        
        return control
    }()
    
    
    

    fileprivate lazy var action_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.white
        control.cornerRadius = 36/375*ScreenWidth/2.0
        control.setTitleForAllStates("立即开始")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(action_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var action_sub_label:UILabel = {
        let control = UILabel.init()
        control.text = "配对不成功不扣甜心币哦！"
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()

    var action_closure:(()->())?
}


extension ST_MatchBeforePropsItemView{
    
    @objc fileprivate func action_handle(){
        
        action_closure?()
        
    }
    
}


//MARK: - Other
class ST_MatchBeforePropsNoImageItemView:UIControl{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.cornerRadius = 16
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            
            make.left.top.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.height.equalTo(TIANCompatibleValue(value: 25))
        }
        
        self.addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(TIANCompatibleValue(value: 2))
            make.height.equalTo(TIANCompatibleValue(value: 17))
        }
        
        
        self.addSubview(priceView)
        priceView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 14))
            make.top.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.height.equalTo(ST_MatchCoinView.height)
        }
        
        self.addSubview(freeLabel)
        freeLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 14))
            make.top.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.height.equalTo(ST_MatchCoinView.height)
            make.width.equalTo(72)
        }
        
        self.addSubview(action_btn)
        action_btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 15))
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 15))
            make.height.equalTo(TIANCompatibleValue(value: 36))
            make.top.equalTo(sub_title_label.snp.bottom).offset(TIANCompatibleValue(value: 10))
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - Bind Model
    var model:ST_MatchBeforePropsNoImageItemViewModel?{
        didSet{
            
            guard let m = model else { return  }
            
            
            titleLabel.text = m.title
            sub_title_label.text = m.subTitle
         
            if m.freeNumber < 1{
                priceView.price = m.price
                priceView.alpha = 1
                freeLabel.alpha = 0
            }else{
                priceView.alpha = 0
                freeLabel.alpha = 1
                freeLabel.text = "今日\(Defaults[.PandaWriteKindStr])"+m.freeNumber.string+"次"
            }
            
            
            action_btn.setTitleColor(m.tintColor, for: UIControl.State.normal)
            
            cover_image_view.bgLayer.colors = m.gradientColors.map({ (color) -> CGColor in
                return color.cgColor
            })
            
        }
    }
    
    
//    MARK: - Properties

    fileprivate lazy var cover_image_view:ST_FWGradientView = {
         let control = ST_FWGradientView()
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 1, y: 0)
        
          return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 18/375.0*ScreenWidth, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value: 12), weight: UIFont.Weight.regular)
        return control
    }()

    fileprivate lazy var freeLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.semibold)
        control.cornerRadius = TIANCompatibleValue(value: 26)/2.0
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.15)
        control.alpha = 0
        return control
    }()

    
    fileprivate lazy var priceView:ST_MatchCoinView = {
        
        let control  = ST_MatchCoinView()
        control.alpha = 0
        return control
    }()
    
    
    

    fileprivate lazy var action_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.white
        control.cornerRadius = 36/375*ScreenWidth/2.0
        control.setTitleForAllStates("立即开始")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(action_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    var action_closure:(()->())?
    @objc fileprivate func action_handle(){
        
        action_closure?()
    }
}


//MARK: - Model

struct ST_MatchBeforePropsNoImageItemViewModel {
    var title:String
    var subTitle:String
    var freeNumber:Int
    var price:Int
    var tintColor:UIColor
    var gradientColors:[UIColor]
}


struct ST_MatchBeforePropsItemViewModel {
    var title:String
    var location:String?
    var subTitle:String
    var price:Int
    var iconUrl:String
    var backgroundImage:UIImage
    var tintColor:UIColor
}

/**
 后台数据模型
 */
class ST_FlashGameDataModel : NSObject{
    
    var busName : String
    var busTip : String
    var cityName : String
    var freeNum : Int
    var id : Int
    var image : String
    var locationImage : String
    var name : String
    var remark : String
    var sort : Int
    var type : Int
    var useCoin : Int

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        busName = json["bus_name"].stringValue
        busTip = json["bus_tip"].stringValue
        cityName = json["city_name"].stringValue
        freeNum = json["free_num"].intValue
        id = json["id"].intValue
        image = json["image"].stringValue
        locationImage = json["location_image"].stringValue
        name = json["name"].stringValue
        remark = json["remark"].stringValue
        sort = json["sort"].intValue
        type = json["type"].intValue
        useCoin = json["use_coin"].intValue
        
        super.init()
    }

}

//MARK: - View
class ST_MatchCoinView: UIControl {
    
    static let height = TIANCompatibleValue(value: 26)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.init(hex: 0x000000, transparency: 0.15)
        
        self.cornerRadius = ST_MatchCoinView.height/2.0
//        self.borderWidth = 1
//        self.borderColor = UIColor.white
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(TIANCompatibleValue(value: 18))
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 5))
        }
//        icon_image_view.backgroundColor = .random
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 28))
            make.right.equalToSuperview().offset(-TIANCompatibleValue(value: 9))
            make.centerY.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("coin_icon"))
          return control
      }()

    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        return control
    }()
    
    var price = 0{
        didSet{
            
            let attrString = NSMutableAttributedString(string: price.string + " ")
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
            attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
            
            let attrUnitString = NSMutableAttributedString(string: "甜心币/次")
            
            
            
            let unitAttr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1),.baselineOffset:2]
            
            attrUnitString.addAttributes(unitAttr, range: NSRange(location: 0, length: attrUnitString.length))
            
            attrString.append(attrUnitString)
            
            titleLabel.attributedText = attrString
            
        }
    }
    
}
